package com.miukoo.aikao.ucenter.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miukoo.aikao.core.dtos.PageReqDto;
import com.miukoo.aikao.core.dtos.PageResDto;
import com.miukoo.aikao.qmanage.api.v1.QuestionApi;
import com.miukoo.aikao.core.pojos.Question;
import com.miukoo.aikao.ucenter.api.v1.WrongQuestionApi;
import com.miukoo.aikao.ucenter.dto.WrongQuestionDto;
import com.miukoo.aikao.ucenter.mapper.WrongQuestionMapper;
import com.miukoo.aikao.ucenter.pojos.WrongQuestion;
import com.miukoo.aikao.ucenter.services.WrongQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
　* 错题集
　* @Author miukoo<miukoo@126.com>
　* @Date 2020/11/19 22:15
　* @Version 1.0
  */
@Service
public class WrongQuestionServiceImpl extends ServiceImpl<WrongQuestionMapper, WrongQuestion> implements WrongQuestionService, WrongQuestionApi {

    @Autowired
    private QuestionApi questionApi;
    @Autowired
    private WrongQuestionMapper wrongQuestionMapper;

    @Override
    public PageResDto<List<WrongQuestion>> listByUserId(PageReqDto<WrongQuestionDto> reqDto, Long userId) {

        //创建分页对象
        Page query = new Page<>(reqDto.getCurrent(), reqDto.getSize());

        //查询条件
        QueryWrapper<WrongQuestion> wrapper = new QueryWrapper<>();
        // 查找用户的错题
        wrapper.lambda().eq(WrongQuestion::getUserId, userId);

        WrongQuestionDto params = reqDto.getParams();
        if(params!=null){
            if(!StringUtils.isEmpty(params.getTitle())){
                wrapper.lambda().like(WrongQuestion::getTitle, params.getTitle());
            }

            if(!StringUtils.isEmpty(params.getExamId())){
                wrapper.lambda().eq(WrongQuestion::getExamId, params.getExamId());
            }
        }

        //获得数据
        IPage<WrongQuestion> page = this.page(query, wrapper);
        //转换结果
        return PageResDto.result(page.getTotal(),page.getRecords());
     }



    @Override
    public void addWrongQuestion(Long examId, Long questionId, Long userId) {

        QueryWrapper<WrongQuestion> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(WrongQuestion::getUserId, userId)
                .eq(WrongQuestion::getExamId, examId)
                .eq(WrongQuestion::getQuestionId, questionId);

        //查找已有的错题信息
        WrongQuestion wq = this.getOne(wrapper, false);

        if (wq == null) { // 新增问题
            Question qu = questionApi.findById(questionId);
            wq = new WrongQuestion();
            wq.setExamId(examId);
            wq.setUserId(userId);
            wq.setTitle(qu.getContent());
            wq.setQuestionId(questionId);
            wq.setWrongCount(1);
            wq.setSort(1);
            wq.setQuestionType(qu.getType());
            this.save(wq);
        } else {
            wrongQuestionMapper.updateWrongCount(wq.getQuestionId());
        }
    }

    @Override
    public Long findNext(Long examId, Long quId, Long userId) {

        Integer sort = 999999;

        if(!StringUtils.isEmpty(quId)){
            QueryWrapper<WrongQuestion> wrapper = new QueryWrapper<>();
            wrapper.lambda()
                    .eq(WrongQuestion::getUserId, userId)
                    .eq(WrongQuestion::getExamId, examId)
                    .lt(WrongQuestion::getQuestionId, quId)
                    .orderByDesc(WrongQuestion::getQuestionId);

            WrongQuestion last = this.getOne(wrapper, false);
            if(last!=null){
                return last.getQuestionId();
            }
        }

        QueryWrapper<WrongQuestion> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(WrongQuestion::getUserId, userId)
                .eq(WrongQuestion::getExamId, examId)
                .orderByDesc(WrongQuestion::getQuestionId);

        WrongQuestion next = this.getOne(wrapper, false);
        if(next != null){
            return next.getQuestionId();
        }
        return null;
    }

}
